/*
 * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
 * License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.aliyun.iot.client;
 
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class IotClient {
	
	private final static Logger logger = LoggerFactory.getLogger(IotClient.class);
	
	private static String accessKeyID;
	
	private static String accessKeySecret;
	
	private static String regionId;
	
	public static DefaultAcsClient client = null;
	
	private static Properties prop = new Properties();

	public synchronized static DefaultAcsClient getClient() {
		if(client!=null){
			return client;
		}
		try {
			prop.load(IotClient.class.getResourceAsStream("/iotcfg.properties"));
			accessKeyID = prop.getProperty("user.accessKeyID");
			accessKeySecret = prop.getProperty("user.accessKeySecret");
			regionId = prop.getProperty("iot.regionId");
			IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
			DefaultProfile.addEndpoint(regionId, regionId, prop.getProperty("iot.productKey"),
					prop.getProperty("iot.domain"));
			// 初始化client
			client = new DefaultAcsClient(profile);
		} catch (Exception e) {
			logger.error("初始化client失败！exception:", e);
		}
		return client;
	}

	public  static String getValue(String key){
		if(StringUtils.isEmpty(key)){
			return null;
		}
		return prop.getProperty(key);
	}
}
